package com.example.shopping_mall.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.shopping_mall.R;
import com.example.shopping_mall.bean.BrandBean;
import com.example.shopping_mall.bean.SecondBean;
import com.example.shopping_mall.bean.SecondLastBean;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;

//秒杀详情适配器
public class ArticleKillAdapter extends RecyclerView.Adapter<ArticleKillAdapter.BaseViewHolder>{

    Context context;
    private List<SecondLastBean> beanList;

    public ArticleKillAdapter(Context context, List<SecondLastBean> beanList) {
        this.context = context;
        this.beanList = beanList;
    }

    @NonNull
    @Override
    public BaseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ArticleHolder(inflate(parent,R.layout.article_kill_detail_list_item));
    }

    @Override
    public void onBindViewHolder(@NonNull BaseViewHolder holder, int position) {
        holder.bindData(beanList.get(position), position);
    }

    @Override
    public int getItemCount() {
        return null==beanList?0:beanList.size();
    }

    private View inflate(ViewGroup parent, int layout) {
        return LayoutInflater.from(parent.getContext()).inflate(layout, parent, false);
    }

    public abstract class BaseViewHolder<DATA> extends RecyclerView.ViewHolder {
        public BaseViewHolder(View itemView) {
            super(itemView);
        }

        public abstract void bindData(DATA data, int position);
    }

    class ArticleHolder extends BaseViewHolder<SecondLastBean>{

        ImageView killImage;
        TextView killtitle;
        TextView killdescribe;
        TextView onsalePrice;
        TextView originalPrice;
//        TextView stock;
//        Button button;

        public ArticleHolder(View itemView) {
            super(itemView);

            killImage=itemView.findViewById(R.id.seconds_kill_details_image);
            killtitle=itemView.findViewById(R.id.seconds_kill_details_title);
            killdescribe=itemView.findViewById(R.id.seconds_kill_details_describe);
            onsalePrice=itemView.findViewById(R.id.seconds_kill_details_onsale_price);
            originalPrice=itemView.findViewById(R.id.seconds_kill_details_original_price);
//            stock=itemView.findViewById(R.id.seconds_kill_details_stock);
//            button=itemView.findViewById(R.id.seconds_kill_details_button);
        }

        @Override
        public void bindData(SecondLastBean lastBean, int position) {
            killtitle.setText(beanList.get(position).getProduct().getName());
            killdescribe.setText(beanList.get(position).getProduct().getDescription());
            onsalePrice.setText(String.valueOf(beanList.get(position).getFlashPromotionPrice()));
            originalPrice.setText(String.valueOf(beanList.get(position).getProduct().getOriginalPrice()));

//            if (lastBean.getFlashPromotionCount() >0) {
//                stock.setText("仅剩"+lastBean.getFlashPromotionCount()+"件");
//                button.setText("马上抢");
//            }else {
//                stock.setText("被抢光啦");
//                button.setText("马上抢");
//            }

            new Thread(new Runnable() {
                @Override
                public void run() {
                    Bitmap bitmap;
                    try {
                        URL url = new URL(lastBean.getProduct().getPic());
                        //Log.d(TAG, "图片"+beans[finalI].getPic());
                        //创建连接方法
                        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                        //读取超时时长，即链接成功后，服务器返回时间
                        connection.setReadTimeout(5000);
                        //连接超时时长，连接服务器超时时间
                        connection.setConnectTimeout(5000);
                        //设置连接
                        connection.connect();
                        //返回结果码
                        if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                            InputStream inputStream = connection.getInputStream();//得到数据流
                            bitmap = BitmapFactory.decodeStream(inputStream);//将流转为bitmap

                            Handler handler=new Handler(Looper.getMainLooper());
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                    killImage.setImageBitmap(bitmap);
                                }
                            });
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}
